package cn.iune.web.erp.controllers;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import cn.iune.config.SecurityService;
import cn.iune.erp.accounting.IAccountVoucherService;
import cn.iune.erp.accounting.IAccountingService;
import cn.iune.erp.autoentry.IEntrySettingService;
import cn.iune.erp.setting.ISettingService;

/*
 * 会计分录模块
 */

@Controller
@RequestMapping(value="/autoentry")
public class AutoentryController {
	
	
	@Autowired
	private IEntrySettingService entrySettingService;
	@Autowired
	private ISettingService settingService;
	
	@Autowired
	private IAccountVoucherService accountVoucherService;
	
	@Autowired
	private IAccountingService accountingService;
	

	
	//自动分录设置--采购进货
	@RequestMapping(value="/entrySetting/cgrk",method=RequestMethod.GET)
	public ModelAndView entryCGRK(){
		ModelAndView mav = new ModelAndView("/appcenter/autoentry/entrySetting/cgrk");
		mav.addObject("path","/autoentry/entrySetting/cgrk");
		return mav;
	}
	
	@RequestMapping(value={
			"/entrySetting/cgrk/create",
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject entrySettingCreate(@RequestBody JSONObject data) {
		data = SecurityService.translateFun(data);
		JSONObject json = entrySettingService.newData(data);
		return json;
	}
	
	@RequestMapping(value={
			"/entrySetting/cgrk/query"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject entrySettingQuery(@RequestBody JSONObject data) {
		data = SecurityService.addDataAccessPerm(data);
		return entrySettingService.queryData(data);
	}

	@RequestMapping(value={
			"/entrySetting/cgrk/save"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject entrySettingSave(@RequestBody JSONObject object) {
		return entrySettingService.saveData(object);
	}
	
	@RequestMapping(value= {
			"/entrySetting/cgrk/delete",
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject entrySettingDelete(@RequestBody JSONObject data) {
		return entrySettingService.deleteData(data);
	}
	
	//复制数据。从BillSetting到BillSettingOrg
	@RequestMapping(value="/entrySetting/loadData",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject entrySettingLoad(@RequestBody JSONObject data) {
		String billType = data.getString("billType");
		String userId = SecurityService.getCurrentPerson().getString("userId");
		String enterpriseNo = SecurityService.getCurrentOgn().getsFID();
		String enterpriseName = SecurityService.getCurrentOgn().getsLongName();
		return settingService.copyBillSetting(billType, userId, enterpriseNo, enterpriseName);
	}

	
	@RequestMapping(value={
			"/draftVoucher/single/query"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject accountVoucherQuery(@RequestBody JSONObject data) {
		data = SecurityService.addDataAccessPerm(data);
		return accountVoucherService.queryData(data);
	}

	@RequestMapping(value={
			"/draftVoucher/single/save"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject accountVoucherSave(@RequestBody JSONObject object) {
		return accountVoucherService.saveData(object);
	}

	@RequestMapping(value={
			"/draftVoucher/single/delete"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject accountVoucherDelete(@RequestBody JSONObject data) {
		return accountVoucherService.deleteData(data);
	}

	@RequestMapping(value={
			"/draftVoucher/single/create"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject accountVoucherCreate(@RequestBody JSONObject data, HttpServletRequest request) {
		data = SecurityService.translateFun(data);
		JSONObject json = accountVoucherService.newData(data);
		return json;
	}


	@RequestMapping(value={
			"/draftVoucher/single/createDetail"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject accountVoucherSaveDetail(@RequestBody JSONObject data) {
		return accountVoucherService.newDataDetail(data);
	}

	@RequestMapping(value={
			"/draftVoucher/single/queryDetail"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject accountVoucherQueryDetail(@RequestBody JSONObject data) {
		return accountVoucherService.queryDataDetail(data);
	}

	

	//分录底稿
	@RequestMapping(value="/draftVoucher/single",method=RequestMethod.GET)
	public ModelAndView draftVoucherSingle(){
		ModelAndView mav = new ModelAndView("/appcenter/autoentry/draftVoucher/single");
		mav.addObject("path","/autoentry/draftVoucher/single");
		return mav;
	}
	
	//由分录底稿生成会计凭证
	@RequestMapping(value = {"/draftVoucher/single/formalKJPZ"}, method = RequestMethod.POST)
	@ResponseBody
	public JSONObject formalKJPZ(@RequestBody JSONObject params, HttpServletRequest request){
		String id = params.getString("rowID");
		
		String cUsercode = SecurityService.getCurrentPerson().getString("cUsercode");
		String cUsername = SecurityService.getCurrentPerson().getString("cUsername");
		String userId = SecurityService.getCurrentPerson().getString("userId");
		
		return accountingService.formalKJPZ(id, cUsercode, cUsername, userId);
	}
	

}
